{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "ba7270f2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "create an instance of: Child\n",
      "name attribute is: init Child\n",
      "create an instance of: Child\n",
      "name attribute is: \n"
     ]
    }
   ],
   "source": [
    "# 定义父类：Parent\n",
    "class Parent(object):\n",
    "    def __init__(self, name):\n",
    "        self.name = name\n",
    "        print(\"create an instance of:\", self.__class__.__name__)\n",
    "        print(\"name attribute is:\", self.name)\n",
    "# 定义子类Child ，继承父类Parent\n",
    "\n",
    "\n",
    "class Child(Parent):\n",
    "    pass\n",
    "\n",
    "\n",
    "# 子类实例化时，由于子类没有初始化，此时父类的初始化函数就会默认被调用\n",
    "# 且必须传入父类的参数name\n",
    "c = Child(\"init Child\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "066160f4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "xiaoyang choubao\n"
     ]
    }
   ],
   "source": [
    "class Parent(object):\n",
    "    def __init__(self, name):\n",
    "        self.name = name\n",
    "\n",
    "\n",
    "class Child(Parent):\n",
    "    def __init__(self,name,l_name):\n",
    "        #self.name = name 这个属性已经在父类中被定义，不需要在子类中重复定义，但是下面要有调用父类属性动作，即super\n",
    "        self.little_name = l_name\n",
    "        super(Child,self).__init__(name)\n",
    "        #Parent.__init__(self,name)\n",
    "    def info(self):\n",
    "        print(self.name,self.little_name)\n",
    "        \n",
    "\n",
    "c = Child(\"xiaoyang\", \"choubao\")\n",
    "c.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "335804d6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Bruce is 25 years old and he weights 60\n",
      "('name', 'age', 'weight')\n",
      "('name', 'age', 'weight')\n"
     ]
    }
   ],
   "source": [
    "class person(object):\n",
    "    __slots__ = (\"name\", \"age\", \"weight\")\n",
    "    slots = (\"name\", \"age\", \"weight\") #今日份耻辱\n",
    "    def __init__(self, name, age, weight):\n",
    "        self.name = name\n",
    "        self.age = age\n",
    "        self.weight = weight\n",
    "    \n",
    "    @classmethod\n",
    "    def info(cls):\n",
    "        print(cls.slots)\n",
    "\n",
    "\n",
    "Bruce = person(\"Bruce\", 25, 60)\n",
    "print(\"%s is %d years old and he weights %s\" %\n",
    "      (Bruce.name, Bruce.age, Bruce.weight))\n",
    "Bruce.tall = 180\n",
    "print(person.slots)\n",
    "person.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "2c8f98bb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Bruce is 25 years old and he weights 60\n",
      "Bruce is 25 years old and he weights 60 and he's tall is 180\n"
     ]
    },
    {
     "ename": "AttributeError",
     "evalue": "'person' object has no attribute 'appearance'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-16-ba19fef89313>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m     12\u001b[0m \u001b[0mBruce\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mtall\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m180\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     13\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"%s is %d years old and he weights %s and he\\'s tall is %s\"\u001b[0m \u001b[1;33m%\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mBruce\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mname\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mBruce\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mage\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mBruce\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mweight\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mBruce\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mtall\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 14\u001b[1;33m \u001b[0mBruce\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mappearance\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m'handsome'\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mAttributeError\u001b[0m: 'person' object has no attribute 'appearance'"
     ]
    }
   ],
   "source": [
    "#如果子类本身也有_ _ slots_ _ 属性，子类的属性就是自身的 _ _ slots _ _ 加上父类的_ _ slots_ _\n",
    "#子类没有，不继承父类slots，即没有限制\n",
    "#子类有，父类没有，父类的实例数据属性能被直接继承用就ok\n",
    "class human(object):\n",
    "    __slots__ = (\"tall\")\n",
    "class person(human):\n",
    "    __slots__ = (\"name\", \"age\",\"weight\")\n",
    "    def __init__(self, name, age, weight):\n",
    "        self.name = name\n",
    "        self.age = age\n",
    "        self.weight = weight\n",
    "Bruce = person(\"Bruce\", 25,60)        \n",
    "print(\"%s is %d years old and he weights %s\" %(Bruce.name, Bruce.age,Bruce.weight))\n",
    "Bruce.tall = 180\n",
    "print(\"%s is %d years old and he weights %s and he\\'s tall is %s\" %(Bruce.name, Bruce.age,Bruce.weight,Bruce.tall))\n",
    "Bruce.appearance = 'handsome'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "d86a346d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "new <class '__main__.A'>\n",
      "init <class '__main__.A'>\n"
     ]
    }
   ],
   "source": [
    "#我们定义一个类，并生成初始化_ _ init _ _ 对象函数和 _ _ new _ _对象函数：\n",
    "\n",
    "class A(object):\n",
    "    def __init__(self,*args, **kwargs):\n",
    "        print(\"init %s\" %self.__class__) \n",
    "    def __new__(cls,*args, **kwargs):\n",
    "        print(\"new %s\" %cls) \n",
    "        return object.__new__(cls, *args, **kwargs)\n",
    "\n",
    "a = A()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "11e98b82",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
